package com.example.testjenkins.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Joey_Wang
 */
public class CompletableFutureTest {
    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

//        CompletableFuture uCompletableFuture = CompletableFuture.runAsync(() -> {
//
//        }, executorService);

        try {
            CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                int a = 1 / 0;
                return "1";
            }, executorService);

            stringCompletableFuture.whenComplete((result, exception) -> {
                System.out.println(result);
                System.out.println("1" + exception);
            }).exceptionally(e -> {
                System.out.println("2" + e);
                return String.valueOf(e);
            });

            System.out.println("end");
        } finally {
            executorService.shutdown();
        }
    }
}
